package business_opportunity

import (
	"context"
	"errors"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/business_opportunity/oppty_status"
	"git.myscrm.cn/golang/stark/v4"
)

type opptyStatusServer struct {
	opptyStatusService oppty_status.OpptyStatusServiceIface
}

func NewOpptyStatusServer(opptyStatusService oppty_status.OpptyStatusServiceIface) pb.OpptyStatusServiceServer {
	return &opptyStatusServer{
		opptyStatusService: opptyStatusService,
	}
}

// GetOpptyStatus 获取数据状态
func (s *opptyStatusServer) GetOpptyStatus(ctx context.Context, request *pb.GetOpptyStatusRequest) (*pb.GetOpptyStatusResponse, error) {
	var errMsg string
	if request.GetDataSource() == "" {
		return &pb.GetOpptyStatusResponse{}, nil
	}
	if request.GetDataSourceId() == "" {
		return &pb.GetOpptyStatusResponse{}, nil
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	res, err := s.opptyStatusService.GetOpptyStatus(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "get-bigdata-status失败：%s", err.Error())
		return nil, err
	}
	return res, nil
}

// GetOpptyStatusBatch 批量获取数据状态
func (s *opptyStatusServer) GetOpptyStatusBatch(ctx context.Context, request *pb.GetOpptyStatusBatchRequest) (*pb.GetOpptyStatusBatchResponse, error) {
	if request.GetDataSource() == "" {
		return &pb.GetOpptyStatusBatchResponse{}, nil
	}
	if len(request.DataSourceId) <= 0 {
		return &pb.GetOpptyStatusBatchResponse{}, nil
	}

	res, err := s.opptyStatusService.GetOpptyStatusBatch(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "get-bigdata-status失败：%s", err.Error())
		return nil, err
	}
	data := &pb.GetOpptyStatusBatchResponse{}
	data.List = res
	return data, nil
}

// UpdateOpptyStatus 更新数据状态
func (s *opptyStatusServer) UpdateOpptyStatus(ctx context.Context, request *pb.UpdateOpptyStatusRequest) (*pb.UpdateOpptyStatusResponse, error) {
	if request.GetDataSource() == "" {
		return &pb.UpdateOpptyStatusResponse{}, nil
	}
	if request.GetDataSourceId() == "" {
		//有些场景不传data_source_id
		return &pb.UpdateOpptyStatusResponse{}, nil
	}

	res, err := s.opptyStatusService.UpdateOpptyStatus(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "update-bigdata-status失败：%s", err.Error())
		return nil, err
	}
	data := &pb.UpdateOpptyStatusResponse{
		Success: res,
	}
	return data, nil
}
